﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Windows;
using Labthink.PCSoft.UniversalHost4.Model;
using Labthink.PCSoft.UniversalHost4.Utility;
using Microsoft.Reporting.WinForms;

namespace Labthink.PCSoft.UniversalHost4.Instrument.Forms
{
    public partial class FrmSignalPrint
    {
        public List<Esignal> Signals { get; set; }
        public string TestIds { get; set; }

        public FrmSignalPrint()
        {
            InitializeComponent();
        }

        private void FrmSignalPrint_OnLoaded(object sender, RoutedEventArgs e)
        {
            var rps = new List<ReportParameter>
            {
                new ReportParameter("SignalRecord", LanguageManager.GetLanguageValue("SignalRecord")),
                new ReportParameter("PrintInfo",
                    Common.CurrentUser.UserName + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm")),
                new ReportParameter("DeviceId",
                    LanguageManager.GetLanguageValue("SignalDeviceId") + " : " + Signals.First().DeviceId),
                new ReportParameter("TestId", LanguageManager.GetLanguageValue("TestId")+" : " +TestIds),
                new ReportParameter("UserName", LanguageManager.GetLanguageValue("SignalName")),
                new ReportParameter("SignalTime", LanguageManager.GetLanguageValue("SignalTime")),
                new ReportParameter("Signal", LanguageManager.GetLanguageValue("SignalStatus")),
                new ReportParameter("Remark", LanguageManager.GetLanguageValue("Remark"))
            };
            var dt = new DataTable();
            dt.Columns.Add("UserName", typeof(string));
            dt.Columns.Add("Date", typeof(string));
            dt.Columns.Add("SignalType", typeof(string));
            dt.Columns.Add("Remark", typeof(string));
            foreach (var s in Signals)
            {
                var dr = dt.NewRow();
                dr["UserName"] = s.UserName;
                dr["Date"] = s.Date.ToString("yyyy-MM-dd HH:mm");
                dr["SignalType"] = LanguageManager.GetLanguageValue("ESignaled" + s.SignalType);
                dr["Remark"] = s.Remark;
                dt.Rows.Add(dr);
            }
            ReportViewer.LocalReport.ReportPath = Common.LocalPath+ "\\SignalRecord.rdlc";
            ReportViewer.Messages = new Report.ReportLanguage();
            ReportViewer.LocalReport.DataSources.Clear();
            ReportViewer.LocalReport.SetParameters(rps.ToArray());
            ReportViewer.LocalReport.DataSources.Add(new ReportDataSource("ES", dt));
            ReportViewer.RefreshReport();
        }

        private void ReportViewer_OnRenderingBegin(object sender, CancelEventArgs e)
        {
            foreach (RenderingExtension extension in ReportViewer.LocalReport.ListRenderingExtensions())
            {
                if (extension.Name == "WORDOPENXML")
                {
                    //extension.Visible = false; // Property is readonly...
                    FieldInfo fi = extension.GetType().GetField("m_isVisible", BindingFlags.Instance | BindingFlags.NonPublic);
                    if (fi != null) fi.SetValue(extension, false);
                }
                if (extension.Name == "EXCELOPENXML")
                {
                    //extension.Visible = false; // Property is readonly...
                    FieldInfo fi = extension.GetType().GetField("m_isVisible", BindingFlags.Instance | BindingFlags.NonPublic);
                    if (fi != null) fi.SetValue(extension, false);
                }
            }
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
